/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.p2psimulator.util;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * @author heekinho
 */
public class ConsoleLogger extends JPanel {

	private Style normalStyle;
	private Style errorStyle;
	private Style infoStyle;


	private StyledDocument document;

    /** Creates new form Console */
    public ConsoleLogger() {
        initComponents();
		consoleTextPane.getEditorKit().createDefaultDocument();

		/* Inicialização dos estilos */
		document = (StyledDocument) consoleTextPane.getDocument();
		normalStyle = document.addStyle("normalStyle", null);
		StyleConstants.setFontSize(normalStyle, 11);

		errorStyle  = document.addStyle("errorStyle", normalStyle);
		infoStyle   = document.addStyle("infoStyle", normalStyle);

		/* Configuração dos estilos */
		StyleConstants.setForeground(errorStyle, Color.red);
		StyleConstants.setBold(errorStyle, true);

		StyleConstants.setForeground(infoStyle, Color.GRAY);

		/* Inicializando Console */
		addInfo("init: \n");
    }

	/**
	 * Adiciona texto colocando quebra de linha ao console
	 * @param text Texto a ser inserido
	 * @param style Estilo desejado
	 */
	private void _addText(String text, Style style){
		try {
			document.insertString(document.getLength(), text, style);
		} catch (BadLocationException ex) {
			Logger.getLogger(ConsoleLogger.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Adiciona texto comum ao console
	 * @param text
	 */
	public void addText(String text){
		_addText(text+"\n", normalStyle);
	}

	/**
	 * Lança um erro no console
	 * @param text
	 */
	public void addError(String text){
		_addText(text+"\n", errorStyle);
	}

	/**
	 * Lança uma "informação interna" no console
	 * @param text
	 */
	public void addInfo(String text){
		_addText(text, infoStyle);
	}

	/**
	 * Limpa a área de texto
	 */
	public void clearText(){
		consoleTextPane.setText("");
	}


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        consoleScroll = new javax.swing.JScrollPane();
        consoleTextPane = new javax.swing.JTextPane();

        consoleTextPane.setEditable(false);
        consoleScroll.setViewportView(consoleTextPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(consoleScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(consoleScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JScrollPane consoleScroll;
    private javax.swing.JTextPane consoleTextPane;
    // End of variables declaration

}
